package com.tjmc.tiomsresource.parser;

import com.tjmc.tiomscore.model.TranPtnHwTpl;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Luc
 * @version 1.0
 * @date 2020-05-11 17:56
 */

@Component("cmTplParser")
public class CMTplParser extends AbstractParser<TranPtnHwTpl> {

    public List<TranPtnHwTpl> parseXML(List<Element> objectElements) {
        if(null == objectElements || objectElements.size() == 0) {
            return null;
        }
        List<TranPtnHwTpl> tranPtnHwTplList = new ArrayList<>();
        int j = 0;
        for(Element objectElement : objectElements) {
            if(++j == PRINT_LOG_SPLIT || j == objectElements.size()) {
                logger.info("解析：" + j + ", size=" + objectElements.size());
            }
            String rmUID = objectElement.attributeValue("rmUID");
            TranPtnHwTpl tranPtnHwTpl = new TranPtnHwTpl();
            tranPtnHwTpl.setRmuid(rmUID);
            List<Element> vElements = objectElement.elements("V");
            if(null == vElements || vElements.size() == 0) {
                continue;
            }
            for(Element vElement : vElements) {
                int i = Integer.parseInt(vElement.attributeValue("i"));
                switch(i) {
                    case 1:
                        tranPtnHwTpl.setNativename(vElement.getTextTrim());
                        break;
                    case 2:
                        tranPtnHwTpl.setAendnermuid(vElement.getTextTrim());
                        break;
                    case 3:
                        tranPtnHwTpl.setZendnermuid(vElement.getTextTrim());
                        break;
                    case 4:
                        tranPtnHwTpl.setAendportrmuid(vElement.getTextTrim());
                        break;
                    case 5:
                        tranPtnHwTpl.setZendportrmuid(vElement.getTextTrim());
                        break;
                    case 6:
                        tranPtnHwTpl.setRate(vElement.getTextTrim());
                        break;
                    case 7:
                        tranPtnHwTpl.setDirection(vElement.getTextTrim());
                        break;
                    case 8:
                        tranPtnHwTpl.setReality(vElement.getTextTrim());
                        break;
                }
            }
            tranPtnHwTplList.add(tranPtnHwTpl);
        }
        return tranPtnHwTplList.size()==0?null:tranPtnHwTplList;
    }
}
